package com.boyu.backend;

import com.boyu.common.BaseController;
import com.boyu.common.OptionConst;
import com.boyu.common.SystemConst;
import com.boyu.common.model.*;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;

import java.util.List;

/**
 * _OptionController
 */
public class _ProductController extends BaseController {

    public void index() {
        String para = getPara(0);
        Integer pageNum = 1;
        if (!para.equals("index")) {
            pageNum = Integer.parseInt(para);
        }
        Page<Product> productPage = Product.dao.paginate(pageNum,20,"select *", "from t_product order by id asc");
        List<ProductAttr> productAttrList = ProductAttr.dao.find("select * from t_product_attr");
        setAttr("productAttrList",productAttrList);
        setAttr("productPage",productPage);
        render(INDEX_HTML);
    }

    public void add() {
        List<ProductAttr> productAttrList = ProductAttr.dao.find("select * from t_product_attr");
        setAttr("productAttrList",productAttrList);
        render(ADD_HTML);
    }

    public void edit() {
        long id = getParaToLong(ID);
        Product byId = Product.dao.findById(id);
        setAttr("model", byId);
        List<ProductAttr> productAttrList = ProductAttr.dao.find("select * from t_product_attr");
        setAttr("productAttrList",productAttrList);
        render(EDIT_HTML);
    }

    public void save() {
        UploadFile fileName = getFile("newImg", SystemConst.UPLOAD_PRODUCT);
        Product model = getModel(Product.class, "");
        if (fileName != null) {
            model.setImage(SystemConst.UPLOAD_PRODUCT+"/"+fileName.getFileName());
        }
        if (model.getId() != null) {
            model.update();
        } else {
            model.save();
        }
        redirect(INDEX);
    }
    public void del() {
        Product byId = Product.dao.findById(getParId());
        byId.setIsDel(1);
        byId.save();
        redirect(INDEX);
    }
}





